package com.lanou.bookstore.user.web.servlet;

import com.lanou.bookstore.user.domain.User;
import com.lanou.bookstore.user.service.RegisterService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

@WebServlet(name = "RegisterServlet", urlPatterns = "/register.action")
public class RegisterServlet extends HttpServlet {
    private RegisterService service = new RegisterService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

//        String username = request.getParameter("username");
//        String password = request.getParameter("password");
//
//        User user = new User();
//        user.setUsername(username)
//                .setPassword(password);
//
//
//        if (service.register(user)) {
//            // 注册成功
//            response.sendRedirect("/jsps/user/login.jsp");
//        } else {
//            fieldMsg(request, response, "注册失败");
//        }
        String username=request.getParameter("username");
        String password=request.getParameter("password");

        User user =new User();
        user.setUsername(username)
                .setPassword(password);
        if (service.register(user)){
            response.sendRedirect("/jsps/user/login.jsp");
        }else {
            fieldMsg(request,response,"注册失败");
        }

    }

    private void fieldMsg(HttpServletRequest request,
                          HttpServletResponse response,
                          String msg) throws ServletException, IOException {

        request.setAttribute("msg", msg);

        request.getRequestDispatcher("/jsps/msg.jsp").forward(request, response);
    }
}


